<?php
namespace app\orders\controller;

use app\common\ErrorCodes;
use app\orders\model\Admins;
use think\Controller;
use think\Db;
use org\Verify;
use app\orders\crypt\AesCrypt;
use think\Lang;

/*
*登录控制器
*/
class Login extends Controller {

    public function admin(){
        $a = new Admins();
        return $a;
    }
    public function index(){
        return $this->fetch('login/index');
    }
    public function base(){
        $base = new Base();
        return $base;
    }
    public function logs(){
        $logs = new UserLog();
        return $logs;
    }


    //后台登录
    public function login(){
        $admin['username'] = input('username');
        $admin['password'] = input('password');
        $hex = new AesCrypt();
        $admin['password'] = $hex->encrypt($admin['password']);
        $hasAdmin = $this->admin()->findById(array('username'=>$admin['username']));
        //var_dump($hasAdmin);exit();
        if (!$hasAdmin){
            return $this->base()->show(array(), ErrorCodes::$FAIL, Lang::get('no user exist'));
//            return $this->error(Lang::get('no user exist'));
        }

        if ($admin['password'] != $hasAdmin['password']){
            return $this->base()->show(array(), ErrorCodes::$FAIL, Lang::get('pass wrong'));
//            return $this->error(Lang::get('pass wrong'));
        }
        /*$code = input('code');
        //var_dump($username);exit;
        $verify = new Verify();
        $check = $verify->check($code);
        if (!$check){
            return $this->base()->show(array(), ErrorCodes::$FAIL, Lang::get('code error'));
//            return $this->error(Lang::get('code error'));
        }*/
        session('orders_user', $admin['username']);
        session('orders_surname', $hasAdmin['surname']);
        session('orders_status', $hasAdmin['status_u']);     //保存用户权限，判断是管理员还是用户。
        session('orders_saleid', $hasAdmin['sale_id']);
        $url = 'Index/index';
        $hasAdmin['menus'] = $this->base()->getAuth($admin['username']);
        return $this->base()->show($hasAdmin, ErrorCodes::$SUCCESS,'登录成功');

    }
    //验证码
    public function verify(){
        $verify = new Verify();
        $verify->imageH = 36;
        $verify->imageW = 140;
        $verify->length = 4;
        $verify->useNoise = false;
        $verify->fontSize = 18;
        $verify->fontttf = '4.ttf';
        return $verify->entry();
    }

    public function out(){
        session('orders_user', null);
        session('orders_surname', null);
        session('orders_status', null);
        session('orders_saleid', null);
        return $this->base()->show(array());
    }




    
}